#include <iostream>
#include <vector>
#include "blockqueue.hpp"
#include "Task.hpp"
#include <ctime>
#include <unistd.h>

void* Consumer(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        Task t;
        bq->Pop(&t);
        // std::cout<<"This is "<<t<<std::endl;
        t();
        // usleep(10000);
        // sleep(1);
    }
}

void* Productor(void* args)
{
    BlockQueue<Task>* bq = static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        int x = rand()%100+1, y = rand()%100+1;
        bq->Enter(Task(x,y));
        //std::cout<<"Enter "<<t<<std::endl;
    }
}

int main()
{
    srand((unsigned int)time(nullptr));
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    std::vector<pthread_t> c(10), p(8);
    for(auto& _p : p)
        pthread_create(&_p, nullptr, Productor, bq);
    for(auto& _c : c)
        pthread_create(&_c, nullptr, Consumer, bq);
    for(auto& _p : p)
        pthread_join(_p, nullptr);
    for(auto& _c : c)
        pthread_join(_c, nullptr);

    return 0;
}